<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.i_id ? (isView ? '查看回执' : '编辑回执') : '批量上传回执'" v-model="visible" align-center width="1200px" :z-index="2000"
    @close="handleClose">
    <div class="my-2 flex flex-col gap-3">
      <VUpload v-model="form.contract_list" edit multiple :params="{is_origin: 2}" :types="['jpg', 'jpeg', 'png', 'pdf']" :disabled="!!form.i_id && isView" title="开票合同"
        class="mb-3" />
      <VUpload v-model="form.deliver_list" edit multiple :params="{is_origin: 2}" :types="['jpg', 'jpeg', 'png', 'pdf']" :disabled="!!form.i_id && isView" title="送货清单" />
      <VUpload v-model="form.invoice_list" edit multiple :params="{is_origin: 2}" :types="['jpg', 'jpeg', 'png', 'pdf']" :disabled="!!form.i_id && isView" title="发票文件" />
    </div>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button v-if="!form.i_id || !isView" type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const isView = ref(false)

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (i_ids, row, view) => {
  isView.value = view
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  visible.value = true
  console.log('row', row)
  if (row) {
    form.value = row
    form.value.i_ids = [row.i_id]
  } else {
    form.value = { i_ids, contract_list: [], deliver_list: [], invoice_list: [] }
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  loadings.submit = true
  const { i_ids, contract_list, deliver_list, invoice_list } = form.value
  api.upload({ i_ids, contract_list, deliver_list, invoice_list }).then(res => {
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>